/*
 * Digital Audio Mobile - A multi track studio for Windows Mobile
 * Copyright (C) 2008 David Olsson
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include "AboutDialog.h"
#include "AudioBuffer.h"
#include "Composition.h"
#include "CompositionInfoControl.h"
#include "DamDialog.h"
#include "TrackInfoControl.h"
#include "TrackRollControl.h"

DamDialog::DamDialog(HINSTANCE hInstance, InputDevice *inDevice, OutputDevice *outDevice) throw(): Dialog(hInstance, TEXT("Digital Audio Mobile"), CS_CLOSE), m_inDevice(inDevice), m_outDevice(outDevice), m_outBuffer1(0), m_outBuffer2(0), m_compositionInfo(0), m_trackInfo(0), m_trackRoll(0), m_composition(0) {
    m_outDevice->addBufferHandler(this);

    m_outBuffer1 = m_outDevice->createCompatibleBuffer(BUFFERSIZE);
    m_outBuffer2 = m_outDevice->createCompatibleBuffer(BUFFERSIZE);

    m_composition = new Composition();

    m_compositionInfo = new CompositionInfoControl(this, m_composition, m_inDevice, m_outDevice);
    m_trackInfo = new TrackInfoControl(this, height() - m_compositionInfo->height());
    m_trackRoll = new TrackRollControl(this, height() - m_compositionInfo->height() - m_trackInfo->height(), m_composition);

    HMENU hMenu = enableMenu(TEXT("Options"));
    AppendMenu(hMenu, MF_ENABLED | MF_STRING, ID_SETTINGS, TEXT("Settings..."));
    AppendMenu(hMenu, MF_SEPARATOR, 0, 0);
    AppendMenu(hMenu, MF_ENABLED | MF_STRING, ID_ABOUT, TEXT("About..."));
}

DamDialog::~DamDialog() {
    if(m_composition != 0) {
        delete(m_composition);
    }
    if(m_trackRoll != 0) {
        delete(m_trackRoll);
    }
    if(m_trackInfo != 0) {
        delete(m_trackInfo);
    }
    if(m_compositionInfo != 0) {
        delete(m_compositionInfo);
    }

    if(m_outDevice != 0) {
        delete(m_outDevice);
    }
    if(m_outBuffer2 != 0) {
        delete(m_outBuffer2);
    }
    if(m_outBuffer1 != 0) {
        delete(m_outBuffer1);
    }
    if(m_inDevice != 0) {
        delete(m_inDevice);
    }
}

void DamDialog::setCurrentTrack(Track *track) throw() {
    m_trackInfo->setCurrentTrack(track);
}

void DamDialog::togglePlay() throw() {
    if(m_outDevice->isPlaying()) {
        m_outDevice->stop();
        m_trackRoll->resetSongPosition();
    }
    else {
        m_clickGen.reset(m_composition->beatsPerMinute(), m_composition->beatsPerMeasure(), m_composition->beatNoteType());
        m_clickGen.generateClicks(m_outBuffer1);
        m_outDevice->enqueueBuffer(m_outBuffer1);
        m_clickGen.generateClicks(m_outBuffer2);
        m_outDevice->enqueueBuffer(m_outBuffer2);
        m_outDevice->play();
    }
}

bool DamDialog::close(CloseCommand cc) throw() {
    m_outDevice->stop();
    return(true);
}

void DamDialog::controlNotification(HWND hWnd, WPARAM notification) throw() {
}

void DamDialog::menuNotification(unsigned int itemId) throw() {
    if(itemId == ID_SETTINGS) {
    }
    else if(itemId == ID_ABOUT) {
        AboutDialog aboutDlg(GetModuleHandle(0));
        aboutDlg.open();
    }
}

void DamDialog::started(Device *device) throw() {
}

void DamDialog::stopped(Device *device) throw() {
}

void DamDialog::bufferCompleted(Device *device, const AudioBuffer *buffer) throw() {
    if(buffer == m_outBuffer1) {
        m_clickGen.generateClicks(m_outBuffer1);
        m_outDevice->enqueueBuffer(m_outBuffer1);
    }
    else if(buffer == m_outBuffer2) {
        m_clickGen.generateClicks(m_outBuffer2);
        m_outDevice->enqueueBuffer(m_outBuffer2);
    }
    m_trackRoll->updateSongPosition(MusicTime(BUFFERSIZE, buffer->samplesPerSecond()));
}

const unsigned int DamDialog::BUFFERSIZE = 8192;
const unsigned int DamDialog::ID_SETTINGS = 1;
const unsigned int DamDialog::ID_ABOUT = 2;
